
<?php
	//ФУНКЦІЯ РЕДАКТИРОВАНИЯ ПОЛЯ "Нужна запчасть"
	function edit_job_do(){//ajax редактирование поля нужна запчасть
		//$query = 'SELECT job FROM orders WHERE id = "'.$_GET['id'].'"';
		//$res=mysql_query($query);
		//$row=mysql_fetch_array($res);
		
			$all_job = $_GET['newdop'];
			$strSQL = 'UPDATE orders SET job = "'.$all_job.'" WHERE id = "'.$_GET['id'].'"';
			mysql_query($strSQL);
			echo $_GET['newdop'];
		
		exit();
	}
	//END!!
	
	function ord_bool(){//заказал
		$strSQL = 'UPDATE orders SET job_bool = "1" WHERE id = "'.$_GET['id'].'"';
		mysql_query($strSQL);

		header("Location: /?action=list_orders", true, 303);
	}
	
	function come_bool(){//пришла
		$strSQL = 'UPDATE orders SET job_bool = "3" WHERE id = "'.$_GET['id'].'"';
		mysql_query($strSQL);

		header("Location: /?action=list_orders", true, 303);
	}
	
		function no_come_bool(){//не пришла
		$strSQL = 'UPDATE orders SET job_bool = "2" WHERE id = "'.$_GET['id'].'"';
		mysql_query($strSQL);

		header("Location: /?action=list_orders", true, 303);
	}
	
	function history(){//история
		$GLOBALS['h1'] = "Моя история";
		$date = time();
		$query = "SELECT * FROM orders WHERE (id_master!=0 AND job_bool=2) OR (id_master!=0 AND job_bool=3)";
		$sql = mysql_query($query) or die(mysql_error());
	
		
		$GLOBALS['content'] .= '
		<div class="row-fluid">
			<table class="table table-bordered">
			<thead> 
				<tr style="text-align:center;">
					<th>Статус</th>
					<th>Нужна запчасть</th>
					<th>№ заказа</th>
					<th>Сц</th>
					<th>Модель</th>
					<th>Стоимость</th>
					<th>Балы в рублях</th>
					<th>Дата приема</th>
					<th>Срочно</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
		';
			
		while($row = mysql_fetch_array($sql)){
			$quickly = 'Нет';
			if($row['bool_quickly'] == 1) {$quickly = 'Да';}
			
			$date = date('d.m.Y', $row['date_admision']);
			if ($row['date_admision'] == 0) {$date = '-';}
			
			$status = get_status($row);
			
			$userlink_man = get_user_link($row['id_menedger']);
			if($row['id_menedger'] == 0){$userlink_man = '-';}
			
			$userlink_mast = get_user_link($row['id_master']); 
			if($row['id_master'] == 0){$userlink_mast = '-';}
			
			$GLOBALS['content'] .= <<<TPL
			<tr>
				<td>{$status}</td>
				<td>{$row['job']}</td>
				<td>{$row['numb_order']}<br />({$row['numb_order_base']})</td>
				<td>{$row['service']}</td>
				<td>{$row['model']}</td>
				<td>{$row['price']}</td>
				<td>{$row['points_r']}</td>
				<td>{$date}</td>
				<td>{$quickly}</td>
				<td>
					Менеджер: {$userlink_man}<br/>
					Мастер: {$userlink_mast}<br/>
				</td>
			</tr>	
TPL;
		}

		$GLOBALS['content'] .= '</tbody></table></div>';
		
		make_order_table(0);
	}
	
	function my_table(){//мой стол
		list_orders();
		/*
		$GLOBALS['h1'] = "Моя стол";
		$date = time();
		$query = "SELECT * FROM orders WHERE id_master!=0 AND job_bool=1";
		$sql = mysql_query($query) or die(mysql_error());
		
		$GLOBALS['content'] .= <<<TPL
		<div class="row-fluid">
			
			<script>
			$(document).ready(function(){
				var oTable = $('#dt_gal').dataTable({
					"sDom": "<'row'>t<'row'<'span4'l><'span4'i><'span4'p>>",
				"sPaginationType": "bootstrap",
					"aaSorting": [[8, "desc"]],
					"aoColumns": [
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"sType": "string"},
						{"bSortable": false}
					]
				});
			});
		</script>
			
			<script>
				$(document).ready(function(){
					
					$('.icon-edit.zap_edit').live('click', function(){
						var ths = $(this).closest('.zap_form');
						ths.empty().html('<textarea></textarea><a href="#" class="zap_btn btn">Редактировать</a>');
						$('.zap_btn').click(function(){
							jQuery.ajax({
								url: '/',
								type: 'GET',
								data: {
									'action': 'edit_job_do',
									'id': $(this).closest('tr').attr('rel'),
									'newdop': ths.find('textarea').val()
								},
								success: function(data){
									//alert(data);
									ths.closest('td').find('.zap_info').append(data);
									ths.html('<i class="icon-edit zap_edit" title="Редактировать"></i>');
								}              
							});
							return false;
						});
					});
				});
			</script>
			<table class="table table-bordered">
			<thead> 
				<tr style="text-align:center;">
					<th>Статус</th>
					<th>Нужна запчасть</th>
					<th>№ заказа</th>
					<th>Сц</th>
					<th>Модель</th>
					<th>Стоимость</th>
					<th>Балы в рублях</th>
					<th>Дата приема</th>
					<th>Срочно</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
TPL;
			
		while($row = mysql_fetch_array($sql)){
			$quickly = 'Нет';
			if($row['bool_quickly'] == 1) {$quickly = 'Да';}
			
			$date = date('d.m.Y', $row['date_admision']);
			if ($row['date_admision'] == 0) {$date = '-';}
			
				$come_but = '';
			if ($row['job_bool'] == 1){
				$come_but = '<a href="/?action=come_bool&id='.$row['id'].'" class="btn_act btn btn-mini">Пришла</a>';
			}
	
			$no_come_but = '';
			if ($row['job_bool'] == 1){
				$no_come_but = '<a href="/?action=no_come_bool&id='.$row['id'].'" class="btn_act btn btn-mini">Не придет</a>';
			}
			
			$status = get_status($row);
			
			$GLOBALS['content'] .= <<<TPL
			<tr rel="{$row['id']}">
				<td>{$status}</td>
				<td style="text-align:left;"><div class="zap_info">{$row['job']}</div><div class="zap_form"><i class="icon-edit zap_edit" title="Редактировать"></i></div></td>
				<td>{$row['numb_order']}<br />({$row['numb_order_base']})</td>
				<td>{$row['service']}</td>
				<td>{$row['model']}</td>
				<td>{$row['price']}</td>
				<td>{$row['points_r']}</td>
				<td>{$date}</td>
				<td>{$quickly}</td>
				<td>
					<div>		
						{$come_but}
						{$no_come_but}
					</div>
				</td>
			</tr>	
TPL;
		}

		$GLOBALS['content'] .= '</tbody></table></div>';
	*/
	}
	
	function execute_order() {
		$query = "UPDATE make_orders SET stat='{$_REQUEST['status']}' WHERE id={$_REQUEST['id']}";
		$sql = mysql_query($query) or die(mysql_error());
		
		echo "<meta http-equiv=\"refresh\" content=\"0;url=" . $_SERVER['HTTP_REFERER'] . "\">";
	}
	
	function make_order_table($relay = 0) {
		if ($relay == 1) { // backlog
			$query = "SELECT * FROM make_orders WHERE stat='0'";
			$col3 = '<th style="width: 100px;"></th>';			
		}
		elseif ($relay == 0) {	// for history(all orders)
			$query = "SELECT * FROM make_orders ORDER BY stat";			
			$col3 = '';
		}
		$sql = mysql_query($query) or die(mysql_error());
	
		
	
		$GLOBALS['content'] .= <<<MAKE_ORD
			<table class="table table-bordered" style="margin-top: 50px;">
				<thead> 
					<tr>
						<th style="text-align: left; padding-left: 25px;">Дата заказа</th>
						<th style="text-align: left; padding-left: 25px;">Деталь</th>
						<th style="width: 150px;">Мастер</th>
						<th style="width: 50px;">СЦ</th>
						<th style="width: 300px;">Статус</th>
						{$col3}
					</tr>
				</thead>
				<tbody>
MAKE_ORD;

		if ($relay == 1) {
			while($row = mysql_fetch_array($sql)) {
				$id      = $row['id'];
				$master  = $row['master'];
				$service = $row['service_center'];
				$detail  = $row['text_order'];
				$status  = $row['stat'];	
				$dater = date("d.m.y H:i:s", $row['date_order']);	
				
				if ($status == 0) {	$status = 'Не пришла';}
				elseif ($status == 2) {	$status = 'Пришла';}
				elseif ($status == 1) {	$status = 'Нету';}

				
				$GLOBALS['content'] .= <<<MAKE_ORD
					<tr>
						<td style="text-align: left; padding-left: 25px; width: 150px;">{$dater}</td>
						<td style="text-align: left; padding-left: 25px;">{$detail}</td>
						<td style="width: 150px;">{$master}</td>
						<td style="width: 50px;">{$service}</td>
						<td style="width: 300px;">
							{$status}
						</td>
						<td style="width: 100px;">
							<a href="/?action=execute_order&id={$id}&status=2" class="btn btn-success btn_act" style="margin-bottom: 5px;">Пришла</a>
							<a href="/?action=execute_order&id={$id}&status=1" class="btn btn-success btn_act" style="margin-bottom: 0px;">Не придет</a>
						</td>
					</tr>
MAKE_ORD;
			}
		} else {
			while($row = mysql_fetch_array($sql)) {
				$detail = $row['text_order'];
				$status = $row['stat'];
				$service = $row['service_center'];
				$master = $row['master'];
				$dater = date("d.m.y H:i:s", $row['date_order']);
				
				if ($status == 0) {	$status = 'Нужна деталь';}
				elseif ($status == 2) {	$status = 'Деталь на складе';}
				elseif ($status == 1) {	$status = 'Выдал';}
				
				$GLOBALS['content'] .= <<<MAKE_ORD
					<tr>
						<td style="text-align: left; padding-left: 25px; width: 150px;">{$dater}</td>
						<td style="text-align: left; padding-left: 25px;">{$detail}</td>
						<td style="width: 150px;">{$master}</td>
						<td style="width: 50px;">{$service}</td>
						<td style="width: 300px;">
							{$status}
						</td>
					</tr>
MAKE_ORD;
			}
		}

$GLOBALS['content'] .= <<<MAKE_ORD
				</tbody>
			</table>
MAKE_ORD;
	}
	
	function list_orders(){//все заказы (Очередь на ремонт)
	
	make_order_table(1);
	
		if(isset($_GET['filter'])){
			switch ($_GET['filter']){
			case 'all':
				$filter = "";
				break;
			case 'hot':
				$GLOBALS['h1'] = "Cрочные заказы";
				$filter = "AND bool_quickly=1";
				break;		
			case 'old':
				$GLOBALS['h1'] = "Долго лежащие заказы";
				$filter = "AND date_admision<100000";
				break;
			case 'search':
				if(isset ($_GET['query'])){
					$GLOBALS['h1'] = 'Поиск "'.$_GET['query'].'" по вашим заказам';
					$query = $_GET['query'];
					$query = trim($query);
					$query = stripslashes($query); 
					$query = htmlspecialchars($query);
					$filter = "AND (numb_order_base LIKE '%{$query}%' OR numb_order LIKE '%{$query}%' OR model LIKE '%{$query}%' OR client_name LIKE '%{$query}%' OR mobile LIKE '%{$query}%')";//Странная ошибка когда буквы
				}
				break;
			default: 
				$filter = "";
				break;
			}
		}else{
			$filter = "";
		}

			$query = "SELECT * FROM orders WHERE id_master!='0' AND job!='' AND (job_bool='' OR job_bool='1') AND date_giving='0' AND refusal='' AND bool_work=1 AND bool_ready='0' {$filter}";

		$sql = mysql_query($query) or die(mysql_error());
		
		$GLOBALS['content'] .= <<<TPL
		<div class="row-fluid">
		<script>
			$(document).ready(function(){
					var oTable = $('#dt_gal').dataTable({
						"sDom": "<'row'>t<'row'<'span4'l><'span4'i><'span4'p>>",
					"sPaginationType": "bootstrap",
						"aaSorting": [[8, "desc"]],
						"aoColumns": [
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"sType": "string"},
							{"bSortable": false},
							{"bSortable": false}
						]
					});
				});
				
			$(document).ready(function(){
					
				$('.icon-edit.zap_edit').live('click', function(){
					var ths = $(this).closest('.zap_form');
					ths.empty().html('<textarea></textarea><a href="#" class="zap_btn btn">Редактировать</a>');
					$('.zap_btn').click(function(){
						jQuery.ajax({
							url: '/',
							type: 'GET',
							data: {
								'action': 'edit_job_do',
								'id': $(this).closest('tr').attr('rel'),
								'newdop': ths.find('textarea').val()
							},
							success: function(data){
								//alert(data);
								ths.closest('td').find('.zap_info').html(data);
								ths.html('<i class="icon-edit zap_edit" title="Редактировать"></i>');
							}              
						});
						return false;
					});
				});
			});
		</script>
		<table class="table table-bordered" id="dt_gal">
			<thead> 
				<tr>
					<th>Статус</th>
					<th>Нужна запчасть</th>
					<th>№ заказа</th>
					<th>Сц</th>
					<th>Модель</th>
					<th>Стоимость</th>
					<th>Балы в рублях</th>
					<th>Дата приема</th>
					<th>Срочно</th>
					<th></th>
					<th></th>
				</tr>
			</thead>
			<tbody>
TPL;

		while($row = mysql_fetch_array($sql)){
			
			$userlink_man = get_user_link($row['id_menedger']);
			if($row['id_menedger'] == 0){$userlink_man = '-';}
			
			$userlink_mast = get_user_link($row['id_master']); 
			if($row['id_master'] == 0){$userlink_mast = '-';}
			
			$quickly = 'Нет';
			if($row['bool_quickly'] == 1) {$quickly = 'Да';}
			
			$date = date('d.m.Y', $row['date_admision']);
			if ($row['date_admision'] == 0) {$date = '-';}
			
			$ord_but = '';
			if ($row['job'] != '' && $row['job_bool']==0){
				$ord_but = '<a href="/?action=ord_bool&id='.$row['id'].'" class="btn_act btn btn-mini">Заказал</a>';
			}
			
			$come_but = '<a href="/?action=come_bool&id='.$row['id'].'" class="btn_act btn btn-mini">Пришла</a>';
			if ($row['job_bool'] == 3 || $row['job_bool'] == 2){
					$come_but = '';
			}
	
			$no_come_but = '<a href="/?action=no_come_bool&id='.$row['id'].'" class="btn_act btn btn-mini">Не придет</a>';
			if ($row['job_bool'] == 2 || $row['job_bool'] == 3){
				$no_come_but = '';
			}
			
			$status = get_status($row);
			
			$GLOBALS['content'] .= <<<TPL
			<tr rel="{$row['id']}">
				<td>{$status}</td>
				<td style="text-align:left;"><div class="zap_info">{$row['job']}</div><div class="zap_form"><i class="icon-edit zap_edit" title="Редактировать"></i></div></td>
				<td>{$row['numb_order']}<br />({$row['numb_order_base']})</td>
				<td>{$row['service']}</td>
				<td>{$row['model']}</td>
				<td>{$row['price']}</td>
				<td>{$row['points_r']}</td>
				<td>{$date}</td>
				<td>{$quickly}</td>
				<td>
					Менеджер: {$userlink_man}<br/>
					Мастер: {$userlink_mast}<br/>
				</td>
				<td>
					<div>		
						{$ord_but}
						{$come_but}
						{$no_come_but}
					</div>
				</td>
			</tr>
TPL;
		}
		$GLOBALS['content'] .= '</tbody></table></div>';	
		
		
	}
	
	
?>